﻿@echo off
setlocal enabledelayedexpansion

echo [INFO] 刷新环境变量 - ScanOS安装程序

:: 安全设置基础系统路径
set "SAFE_PATH=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;"
set "PATH=%SAFE_PATH%"

:: 从注册表加载系统PATH
for /f "skip=2 tokens=2,*" %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path 2^>nul') do (
    set "REG_PATH=%%B"
    set "PATH=!PATH!;!REG_PATH!"
)

:: 添加ScanOS的Java路径
set "JAVA_HOME_REG="
for /f "tokens=2,*" %%a in ('reg query "HKLM\SOFTWARE\ScanOS" /v JavaHome 2^>nul') do (
    set "JAVA_HOME_REG=%%b"
    set "PATH=!PATH!;%%b\bin"
)

:: 更新系统PATH - 仅当有变化时
if not "!PATH!"=="%PATH%" (
    setx PATH "!PATH!" /M >nul
)

:: 更新JAVA_HOME - 仅当找到值时
if defined JAVA_HOME_REG (
    setx JAVA_HOME "!JAVA_HOME_REG!" /M >nul
)

echo [SUCCESS] 环境变量刷新完成
endlocal
exit /b 0